#include "../mqserver/mq_route.hpp"
#include <gtest/gtest.h>

class Routetest : public testing::Environment
{
    virtual void SetUp() override
    {
    }

    virtual void TearDown() override
    {
    }
};
using namespace csjmq;
TEST(route_test, is_legal_routing_key)
{
    ASSERT_EQ(Router::isLegalRoutingkey(".root.pop.#"), false);
    ASSERT_EQ(Router::isLegalRoutingkey(".root_pop."), true);
    ASSERT_EQ(Router::isLegalRoutingkey("_root.pop.music"), true);
    ASSERT_EQ(Router::isLegalRoutingkey(".ro&t.pop.#"), false);
    ASSERT_EQ(Router::isLegalRoutingkey(".root.*.pop."), false);
}

TEST(route_test, is_legal_binding_key)
{
    ASSERT_EQ(Router::isLegalBindingkey(".root.pop.#"), true);
    ASSERT_EQ(Router::isLegalBindingkey(".root.*.*.pop."), true);
    ASSERT_EQ(Router::isLegalBindingkey("_root.#.#.music"), false);
    ASSERT_EQ(Router::isLegalBindingkey(".root.#.*"), false);
    ASSERT_EQ(Router::isLegalBindingkey(".root.*.#.pop."), false);
    ASSERT_EQ(Router::isLegalBindingkey(".root.*..pop."), true);
}

std::vector<std::string> bkeys = {
    "aaa",
    "aaa.bbb",
    "aaa.bbb",
    "aaa.bbb",
    "aaa.#.bbb",
    "aaa.bbb.#",
    "#.bbb.ccc",
    "aaa.bbb.ccc",
    "aaa.*",
    "aaa.*.bbb",
    "*.aaa.bbb",
    "#",
    "aaa.#",
    "aaa.#",
    "aaa.#.ccc",
    "aaa.#.ccc",
    "aaa.#.ccc",
    "#.ccc",
    "#.ccc",
    "aaa.#.ccc.ccc",
    "aaa.#.bbb.*.bbb"};
std::vector<std::string> rkeys = {
    "aaa",
    "aaa.bbb",
    "aaa.bbb.ccc",
    "aaa.ccc",
    "aaa.bbb.ccc",
    "aaa.ccc.bbb",
    "aaa.bbb.ccc.ddd",
    "aaa.bbb.ccc",
    "aaa.bbb",
    "aaa.bbb.ccc",
    "aaa.bbb",
    "aaa.bbb.ccc",
    "aaa.bbb",
    "aaa.bbb.ccc",
    "aaa.ccc",
    "aaa.bbb.ccc",
    "aaa.aaa.bbb.ccc",
    "ccc",
    "aaa.bbb.ccc",
    "aaa.bbb.ccc.ccc.ccc",
    "aaa.ddd.ccc.bbb.eee.bbb"};
std::vector<bool> result = {
    true,
    true,
    false,
    false,
    false,
    false,
    false,
    true,
    true,
    false,
    false,
    true,
    true,
    true,
    true,
    true,
    true,
    true,
    true,
    true,
    true};

TEST(route_test, route)
{
    for (int i = 0; i < rkeys.size(); i++)
    {
        ASSERT_EQ(Router::route(ExchangeType::TOPIC, rkeys[i], bkeys[i]), result[i]);
    }
}
int main(int argc, char *argv[])
{
    testing::AddGlobalTestEnvironment(new Routetest);
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
